package com.bwie.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.IndexInfoReq;
import com.bwie.common.domain.IndexInfoResp;
import com.bwie.common.result.Result;
import com.bwie.es.service.EsIndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/indexEs")
public class EsIndexController {

    @Autowired
    private EsIndexService esIndexService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/createIndex")
    public Result createIndex(@RequestBody IndexInfoReq indexInfoReq){
        log.info("功能名称:Es索引的创建,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(indexInfoReq));
        Result index = esIndexService.createIndex(indexInfoReq);
        log.info("功能名称:Es索引的创建,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(index));

        return Result.success(index);
    }

    @PostMapping("/deleteIndex")
    public Result deleteIndex(@RequestParam String indexName){
        log.info("功能名称:Es索引的删除,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                indexName);
        Result result = esIndexService.deleteIndex(indexName);
        log.info("功能名称:Es索引的删除,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return Result.success(result);
    }

    @PostMapping("/isExitIndex")
    public Result isExitIndex(@RequestParam String indexName){
        log.info("功能名称:判断Es索引是否存在,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                indexName);
        Boolean exitIndex = esIndexService.isExitIndex(indexName);
        log.info("功能名称:判断Es索引是否存在,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(exitIndex));
        return Result.success(exitIndex);
    }

    @PostMapping("/getIndex")
    public Result<IndexInfoResp> getIndex(@RequestParam String indexName){
        log.info("功能名称:根据索引名称查询,请求URI:{},请求方式{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                indexName);
        IndexInfoResp index = esIndexService.getIndex(indexName);
        log.info("功能名称:根据索引名称查询,请求URI:{},请求方式{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(index));
        return Result.success(index);
    }


    @PostMapping("/updateIndex")
    public Result updateIndex(@RequestBody IndexInfoReq indexInfoReq){
        log.info("功能名称:Es索引的修改,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(indexInfoReq));
        Result result = esIndexService.updateIndex(indexInfoReq);
        log.info("功能名称:Es索引的修改,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }




}
